package adfc.renderer;

import adfc.game.Chunk;

import javax.media.opengl.*;
import javax.swing.*;
import java.awt.*;

import static adfc.game.GameConstants.CHUNK_XY_SIZE;
import static adfc.game.GameConstants.CHUNK_Z_SIZE;
import static javax.media.opengl.GL.*;

/**
 * Created by IntelliJ IDEA.
 * User: aimozg
 * Date: 31.12.11
 * Time: 18:26
 */
public class Renderer {
    JFrame frame;
    GLCanvas canvas;
    private Chunk chunk;

    public Renderer() {
    }

    public void initGui() {
        frame = new JFrame("Another Dwarf Fortress Clone");
        GLCapabilities caps = new GLCapabilities();
        caps.setDoubleBuffered(true);
        caps.setStencilBits(1);
        canvas = new GLCanvas(caps);
        canvas.addGLEventListener(new GLEventListener() {

            public void init(GLAutoDrawable drawable) {
                Renderer.this.init(drawable);
            }

            public void display(GLAutoDrawable drawable) {
                Renderer.this.display(drawable);
            }

            public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
                Renderer.this.reshape(drawable, x, y, width, height);
            }

            public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
                Renderer.this.displayChanged(drawable, modeChanged, deviceChanged);
            }
        });
        frame.add(canvas);
        frame.setSize(640, 480);
        frame.setVisible(true);
        Insets insets = frame.getInsets();
        frame.setSize(640 + insets.left + insets.right,
                480 + insets.top + insets.bottom);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void loop() {
        while (true) {
            canvas.display();
        }
    }

    private void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        gl = drawable.getGL();
        gl.glMatrixMode(GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrtho(0, width, height, 0, -1.0, 1.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    private void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }

    GL gl;

    private void display(GLAutoDrawable drawable) {
        gl = drawable.getGL();

        gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL_MODELVIEW);
        gl.glLoadIdentity();

        // Render heightmap
        renderHeightmap(10.0f, chunk.cacheHeightmap);
    }

    private void renderHeightmap(float scale, int[] heightmap) {
        gl.glPushMatrix();
        gl.glScalef(scale, scale, 1.0f);
        gl.glBegin(GL_QUADS);
        for (int y = 0; y < CHUNK_XY_SIZE; y++) {
            for (int x = 0; x < CHUNK_XY_SIZE; x++) {
                float w = ((float) heightmap[Chunk.xyoffset(x, y)]) / CHUNK_Z_SIZE;
                gl.glColor3f(w, w, w);
                gl.glVertex2f(x, y);
                gl.glVertex2f(x + 1, y);
                gl.glVertex2f(x + 1, y + 1);
                gl.glVertex2f(x, y + 1);
            }
        }
        gl.glEnd();
        gl.glPopMatrix();
    }

    private void init(GLAutoDrawable drawable) {
    }

    public void setOnlyChunk(Chunk chunk) {
        this.chunk = chunk;
    }
}
